﻿//-----------------------------------------------------------------------
// <copyright file="UserProfileRepository.cs" company="FastNET Services">
//     Copyright (c) 2008-2009. All Rights Reserved.
// </copyright>
// <license>
//     GPLv2
// </license>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
//-----------------------------------------------------------------------
#region Usings
using System;
using System.Collections.Generic;
using System.Data.EntityClient;
using System.Linq;
using FastNET.CleanAct.Client.CleanEngineData.Entities;
using FastNET.CleanAct.Common.Repository;
using FastNET.CleanAct.Common.Repository.Interfaces.DataInterfaces.Client;
using FastNET.CleanAct.Common.Repository.Interfaces.Repositories.Client;
#endregion

namespace FastNET.CleanAct.Client.CleanEngineData.Repositories
{
    /// <summary>
    /// General repository for managing UserProfile
    /// </summary>
    internal class UserProfileRepository
        : IUserProfileRepository
    {
        #region CTor/DTor

        /// <summary>
        /// Initializes a new instance of the UserProfileRepository class
        /// </summary>
        /// <param name="connection">Reference to the common EntityConnection for the repository</param>
        internal UserProfileRepository(EntityConnection connection)
        {
            Connection = connection;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets a reference to the common EntityConnection for the repository
        /// </summary>
        protected EntityConnection Connection { get; private set; }

        #endregion

        #region IUserProfileRepository Members

        /// <summary>
        /// Retrieves a single IUserProfileData by looking at the AccountName
        /// </summary>
        /// <param name="accountName">Account name to search by</param>
        /// <returns>IUserProfileData found or null if no account was found</returns>
        /// <exception cref="ArgumentNullException">Thrown if the search value is null or empty</exception>
        public IUserProfileData RetrieveByAccountName(string accountName)
        {
            if (string.IsNullOrEmpty(accountName))
            {
                throw new ArgumentNullException("accountName", "An account name must be provided and cannot be empty");
            }

            using (EngineEntities dataSource = new EngineEntities(Connection))
            {
                UserProfile foundItem = (from
                                             item
                                             in
                                             dataSource.UserProfileSet
                                         where
                                             item.AccountName == accountName
                                         select
                                             item).FirstOrDefault();

                if (foundItem != null)
                {
                    dataSource.Detach(foundItem);
                }

                return foundItem;
            }
        }

        /// <summary>
        /// Checks to see if an IUserProfileData exists by the AccountName
        /// </summary>
        /// <param name="accountName">Account name to search by</param>
        /// <param name="ignoreID">ID of an user profile to ignore during check</param>
        /// <returns>Boolean whether or not an user profile exists in the system</returns>
        /// <exception cref="ArgumentNullException">Thrown if AccountName is null or empty</exception>
        /// <exception cref="InvalidCastException">Thrown if IgnoreID is of the wrong type for this repository</exception>
        public bool AccountNameExists(string accountName, Identity? ignoreID)
        {
            if (string.IsNullOrEmpty(accountName))
            {
                throw new ArgumentNullException("accountName", "Must have an account name to search by");
            }

            using (EngineEntities dataSource = new EngineEntities(Connection))
            {
                if (ignoreID.HasValue)
                {
                    if (!ignoreID.Value.CanFitInGuid())
                    {
                        throw new InvalidCastException("IgnoreID must be of type Guid for this repository");
                    }

                    Guid searchID = ignoreID.Value;
                    return (from
                                account
                                in
                                dataSource.UserProfileSet
                            where
                                account.AccountName == accountName &&
                                account.ProfileID != searchID
                            select
                                account).Count() > 0;
                }

                return (from
                            account
                            in
                            dataSource.UserProfileSet
                        where
                            account.AccountName == accountName
                        select
                            account).Count() > 0;
            }
        }

        #endregion

        #region IRepository<IUserProfileData> Members

        /// <summary>
        /// Retrieves all instances of IUserProfileData from the repository
        /// </summary>
        /// <returns>IEnumerable of the requested IData</returns>
        public IList<IUserProfileData> Retrieve()
        {
            List<IUserProfileData> returnValues = new List<IUserProfileData>();

            using (EngineEntities dataSource = new EngineEntities(Connection))
            {
                IEnumerable<UserProfile> query = from item in dataSource.UserProfileSet select item;

                foreach (UserProfile item in query)
                {
                    returnValues.Add(item);
                    dataSource.Detach(item);
                }
            }

            return returnValues;
        }

        /// <summary>
        /// Retrieves the requested IUserProfileData by an Identity
        /// </summary>
        /// <param name="ID">Identity to retrieve the data by</param>
        /// <returns>IData instance or null if one was not found</returns>
        /// <exception cref="InvalidCastException">Thrown if the identity is of the wrong type for this datasource</exception>
        public IUserProfileData Retrieve(Identity ID)
        {
            if (!ID.CanFitInGuid())
            {
                throw new InvalidCastException(string.Format("Identity for this type of repository requires it to be of Guid Type. Currently is {0}.", ID.UnderlyingType()));
            }

            // This has to be done here because LINQ does not support the inline case
            Guid searchID = ID;

            using (EngineEntities dataSource = new EngineEntities(Connection))
            {
                UserProfile foundItem = (from
                                             item
                                             in
                                             dataSource.UserProfileSet
                                         where
                                             item.ProfileID == searchID
                                         select
                                             item).FirstOrDefault();

                if (foundItem != null)
                {
                    dataSource.Detach(foundItem);
                }

                return foundItem;
            }
        }

        /// <summary>
        /// Deletes the requested IUserProfileData
        /// </summary>
        /// <param name="Data">IUserProfileData to delete from the repository</param>
        /// <exception cref="ArgumentNullException">Thrown if data is null</exception>
        /// <exception cref="InvalidCastException">Thrown if the data is of the wrong type for this datasource</exception>
        /// <exception cref="InvalidOperationException">Thrown if the IUserProfileData is new or already deleted</exception>
        public void Delete(IUserProfileData Data)
        {
            if (Data == null)
            {
                throw new ArgumentNullException("Data", "Must have a valid Data to delete");
            }

            if (!(Data is UserProfile))
            {
                throw new InvalidCastException("Data is not of type UserProfile. Cannot delete this object through this repository");
            }

            if (Data.IsNew)
            {
                throw new InvalidOperationException("Data cannot be new before deleting. There is nothing to delete.");
            }

            if (Data.IsDeleted)
            {
                throw new InvalidOperationException("Data already deleted.");
            }

            using (EngineEntities dataSource = new EngineEntities(Connection))
            {
                dataSource.Attach((UserProfile)Data);
                dataSource.DeleteObject(Data);
                dataSource.SaveChanges();
            }
        }

        /// <summary>
        /// Saves the requested IUserProfileData
        /// </summary>
        /// <param name="Data">IUserProfileData to save to the repository</param>
        /// <exception cref="ArgumentNullException">Thrown if data is null</exception>
        /// <exception cref="InvalidCastException">Thrown if the data is of the wrong type for this datasource</exception>
        public void Save(IUserProfileData Data)
        {
            if (Data == null)
            {
                throw new ArgumentNullException("Data", "Must have a valid Data to delete");
            }

            if (!(Data is UserProfile))
            {
                throw new InvalidCastException("Data is not of type Account. Cannot save this object through this repository");
            }

            if (!Data.IsDirty)
            {
                return;
            }

            using (EngineEntities dataSource = new EngineEntities(Connection))
            {
                if (Data.IsNew)
                {
                    dataSource.AddToUserProfileSet((UserProfile)Data);
                    dataSource.SaveChanges();
                    dataSource.Detach(Data);
                }
                else
                {
                    dataSource.ApplyPropertyChanges("UserProfiles", Data);
                    dataSource.SaveChanges();
                }
            }
        }

        /// <summary>
        /// Creates a new instance of IUserProfileData
        /// </summary>
        /// <returns>New Instance of IUserProfileData</returns>
        public IUserProfileData Create()
        {
            return new UserProfile();
        }

        #endregion
    }
}